package com.lzy.chat.d_controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.chat.a_entity.*;
import com.lzy.chat.c_service.Exception.Exceptions;
import com.lzy.chat.c_service.Exception.ServiceException;
import com.lzy.chat.c_service.GroupService;
import com.lzy.chat.c_service.UserService;
import com.lzy.chat.c_service.WebsocketService;
import com.lzy.chat.e_util.JsonResult;
import com.lzy.chat.g_annotation.CurrentUser;
import com.lzy.chat.g_annotation.NeedToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("group")
public class GroupController extends BaseController {
    private GroupService groupService;

    private UserService userService;

    @Autowired
    public void setGroupService(GroupService groupService) {
        this.groupService = groupService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @NeedToken
    @RequestMapping("createGroup")
    public JsonResult<Void> createGroup(Group group, @CurrentUser User user) {
        groupService.createGroup(group, user.getId());
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("dismissGroup")
    public JsonResult<Void> dismissGroup(Integer gid, @CurrentUser User user) {
        groupService.dismissGroup(gid, user.getId());
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("search")
    public JsonResult<List<Group>> searchGroup(String keyword, Integer index) {
        PageHelper.startPage(index, 10);
        List<Group> list = groupService.getGroupByName(keyword);
        PageInfo<Group> pageInfo = new PageInfo<>(list);
        JsonResult<List<Group>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setData(list);
        return jsonResult;
    }

    @NeedToken
    @RequestMapping("addGroup")
    public JsonResult<Void> addGroup(Integer uid, Integer gid, @CurrentUser User user) {
        groupService.addGroupShip(uid, user.getId(), gid);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("quitGroup")
    public JsonResult<Void> quitGroup(Integer gid, @CurrentUser User user) {
        groupService.deleteGroupShip(gid, user.getId());
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("accept")
    public JsonResult<Void> acceptToJoin(AddGroup addGroup, @CurrentUser User user) {
        groupService.addGroupShip(addGroup.getUid(), user.getId(), addGroup.getGid());
        groupService.dealAddGroup(addGroup.getId(), 1);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("reject")
    public JsonResult<Void> rejectToJoin(Integer id, @CurrentUser User user) {
        groupService.dealAddGroup(id, 2);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("requestToAdd")
    public JsonResult<Void> requestToAddGroup(AddGroup addGroup) throws IOException {
        groupService.requestToAddGroup(addGroup);
        Group group = groupService.getGroupByGid(addGroup.getGid());
        if (group.getNeedVerify() == 2) throw new ServiceException(Exceptions.GroupNotAllowedJoin);
        List<Integer> users = groupService.getGroupManager(addGroup.getGid());
        for (Integer uid : users) {
            Message message = new Message();
            message.setMessageType(6);
            message.setData("1");
            message.setToId(uid);
            WebsocketService.sendToUser(message);
        }
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("getUnDealRequest")
    public JsonResult<List<AddGroup>> getUnDealRequest(@CurrentUser User user) {
        List<Integer> manageGroup = groupService.getOnesManageGroup(user.getId());
        List<AddGroup> addGroups = new ArrayList<>();
        for (Integer gid : manageGroup) {
            List<AddGroup> l = groupService.getUnDealByGid(gid);
            addGroups.addAll(l);
        }
        return new JsonResult<>(ok, addGroups);
    }

    @NeedToken
    @RequestMapping("getMyGroup")
    public JsonResult<List<Group>> getMyGroup(@CurrentUser User user) {
        List<Integer> g = groupService.getOnesGroup(user.getId());
        ArrayList<Group> list = new ArrayList<>();
        for (Integer gid : g) {
            list.add(groupService.getGroupByGid(gid));
        }
        return new JsonResult<>(ok, list);
    }

    @NeedToken
    @RequestMapping("getGroupInfo")
    public JsonResult<Group> getGroupInfo(Integer gid) {
        Group group = groupService.getGroupByGid(gid);
        return new JsonResult<>(ok, group);
    }

    @NeedToken
    @RequestMapping("updateGroupInfo")
    public JsonResult<Void> updateGroupInfo(Group group, @CurrentUser User user) {
        GroupShip groupShip = groupService.getGroupShipByUidAndGid(user.getId(), group.getId());
        if (groupShip.getPermission() < 1) throw new ServiceException(Exceptions.GroupPermissionException);
        groupService.updateGroupInfo(group);
        return new JsonResult<>(ok);
    }

    @NeedToken
    @RequestMapping("isJoin")
    public JsonResult<Integer> isJoinGroup(Integer gid, @CurrentUser User user) {
        GroupShip groupShip = groupService.getGroupShipByUidAndGid(user.getId(), gid);
        int isJoin = -1;
        if (groupShip != null) isJoin = groupShip.getPermission();
        return new JsonResult<>(ok, isJoin);
    }


    @NeedToken
    @RequestMapping("getGroupMember")
    public JsonResult<List<GroupShip>> getGroupMember(Integer gid) {
        List<GroupShip> groupShips = groupService.getGroupShipByGid(gid);
        for (GroupShip g : groupShips) {
            User u = userService.getInfoById(g.getUid());
            g.setUser(u);
        }
        return new JsonResult<>(ok, groupShips);
    }
}
